2023/12/234123字符
遍历函数
forEach()
let arr = [1, 2, 3, 4, 5];
arr.forEach(function (val, index, self) {
console.log(val, index, self, this);
}, {name: 'bozai'}); // froEach 第二个参数改变this指向自身,默认值 undefined
如果要用 forEach 第二个参数,就不要用 es6 中的箭头函数,箭头函数 this 指向函数自身
forEach 还原
Array.prototype.myForEach = function (func) {
var _arr = this, param2 = arguments[1] || undefined;
var newArr = [];
for (var prop in _arr) {
if (_arr.hasOwnProperty(prop)) { // 不对稀疏数组 empty 值做回调
func.apply(param2, [_arr[prop], prop, _arr]);
}
}
}
let arr2 = [
{title : 'aaa', read : 100, hot : true},
{title : 'bbb', read : 100, hot : false},
{title : 'ccc', read : 100, hot : false}
];
filter() 循环过滤 es6 循环过后会返回一个新数组
let newArr2 = arr2.filter(function (val, index, self) {
if (val.title == 'aaa') {
return true; // true:留下,false:抛弃
}
}, {name: 'bozai'}); // 第二个参数改变this指向自身
console.log(newArr2); //--> {title: "aaa", read: 100, hot: true}
filter 还原
Array.prototype.myFilter = function (func) {
var _arr = this, len = _arr.length, param2 = arguments[1] || window, newArr = [];
for (var i = 0; i < len; i++) {
func.apply(param2, [_arr[i], i, _arr]) ? newArr.push(_arr[i]) : '';
}
return newArr;
}
map() 通常用来做数据交互/映射 将原数组做出更改
let newArr3 = arr2.map(function (val, index, self) {
val.read += '10';
return val;
}, {name: 'bozai'}); // 第二个参数改变this指向自身; //--> (3) [1, 1, 1]
console.log(newArr3);
map 还原
Array.prototype.myMap = function (func) {
var _arr = this, len = _arr.length, param2 = arguments[1] || window, newArr = [];
for (var i = 0; i < len; i++) {
newArr.push(func.apply(param2, [_arr[i], i, _arr]));
}
return newArr;
}
every() 数组里面所有的元素都符合条件,返回 true
let arr3 = [1, 3, 5, 7, 9];
let boolean1 = arr3.every(function (val, index, self) {
return val % 2 == 1;
}, {name: 'bozai'});
console.log(boolean); //--> true
every 还原
Array.prototype.myEvery = function (func) {
var _arr = this, len = _arr.length, param2 = arguments[1] || window, flag = true;
for (var i = 0; i < len; i++) {
if (!func.apply(param2, [_arr[i], i, _arr])) {
flag = false;
break;
}
}
return flag;
}
some() 类似查找,数组里面某一个元素符合条件,返回 true
let arr4 = [1, 2, 3, 4, 5];
let boolean2 = arr4.some(function (val, index, self) {
return val % 2 == 1;
}, {name: 'bozai'})
console.log(boolean2) //--> true
reduce()
let arr5 = [1, 2, 3, 4, 5];
let res1 = arr5.reduce((first, next) => {
return first + next;
})
console.log(res1); //--> 15
reduce 还原
Array.prototype.myReduce = function (func, initialValue) {
var _arr = this, len = _arr.length, param2 = arguments[2] || window;
for (var i = 0; i < len; i++) {
initialValue = func.apply(param2, [initialValue, _arr[i], i, _arr]);
}
return initialValue;
}
reduceRight()
let arr5 = [2, 4, 16];
let res2 = arr5.reduceRight((first, next) => {
return first / next;
})
console.log(res2); //--> 2
for(of)
let arr = [1, 2, 3, 4, 5];
for(let index of arr.keys()){ // 返回索引值
console.log(index); //--> 01234
}
for(let item of arr.entries()){ // 返回给定对象自身可枚举属性的键值对数组
console.log(item); //--> [01234][12345]
}
幂运算
console.log(Math.pow(2, 3)); //--> 8
console.log(2**3); //--> 8 es8 新增方法